﻿using System;
using System.Collections.Generic;
using System.Text;

namespace DivShare.Sample.ConsoleApplication
{
    public class Program
    {
        private const string ApiKey = "<API KEY>";
        private const string ApiSecret = "<API SECRET KEY>";

        public static void Main(string[] args)
        {
            try
            {
                string userEmail = "<EMAIL>";
                string userPassword = "<PASSWORD>";
                string sessionId = null;
                WebFileInfo[] wfInfos = null;
                string uploadTicket = null;
                string fileNameToUpload = "sample.png";
                string fileDescriptionToUpload = "SAMPLE UPLOAD";
                string webFileId = null;
                WebFile webFile = null;

                DivShareClient ds = new DivShareClient(ApiKey, ApiSecret);

                Console.Write("Logging on to DivShare as '{0}' ...", userEmail);
                sessionId = ds.LogOn(userEmail, userPassword);
                Console.WriteLine("OK");
                Console.WriteLine("\t Session ID: {0}", sessionId);
                Console.WriteLine();

                Console.Write("Getting the user info ...");
                UserInfo userInfo = ds.GetUserInfo();
                Console.WriteLine("OK");
                Console.WriteLine("\t Email: {0}", userInfo.Email);
                Console.WriteLine("\t First name: {0}", userInfo.FirstName);
                Console.WriteLine();

                Console.Write("Getting the list of files stored at DivShare ...");
                wfInfos = ds.GetUserWebFiles();
                Console.WriteLine("OK");

                for (int idxFile = 0; idxFile < wfInfos.Length; idxFile++)
                {
                    WebFileInfo wfInfo = wfInfos[idxFile];
                    Console.WriteLine("\t {0}. File", idxFile + 1);
                    DisplayWebFileInfo(wfInfo);
                    Console.WriteLine();
                }
                Console.WriteLine();

                Console.Write("Getting an upload ticket ...");
                uploadTicket = ds.GetWebFileUploadTicket();
                Console.WriteLine("OK");
                Console.WriteLine("\t Upload ticket: {0}", uploadTicket);
                Console.WriteLine();

                Console.Write("Uploading file '{0}' to DivShare ...", fileNameToUpload);
                webFileId = ds.UploadWebFile(fileNameToUpload, fileDescriptionToUpload, null, uploadTicket);
                Console.WriteLine("OK");
                Console.WriteLine("\t File ID: {0}", webFileId);
                Console.WriteLine();

                /*
                Console.Write("Getting file info of the uploaded file...");
                wfInfos = ds.GetWebFiles(webFileId);
                Console.WriteLine("OK");
                if (wfInfos.Length > 0)
                    DisplayWebFileInfo(wfInfos[0]);
                Console.WriteLine();
                */

                Console.Write("Downloading the uploaded file ...");
                webFile = ds.DownloadWebFile(webFileId);
                Console.WriteLine("OK");
                Console.WriteLine();

                Console.Write("Logging off from DivShare ...");
                ds.LogOff();
                Console.WriteLine("OK");
            }
            catch (Exception ex)
            {
                Console.WriteLine();
                Console.WriteLine("ERROR: {0}", ex);
            }

            Console.WriteLine("Press enter to exist...");
            Console.ReadLine();
        }

        private static void DisplayWebFileInfo(WebFileInfo wfInfo)
        {
            Console.WriteLine("\t File: {0} ({1})", wfInfo.Name, wfInfo.Id);
            Console.WriteLine("\t Description: {0}", wfInfo.Description);
            Console.WriteLine("\t Size: {0}", wfInfo.Size);
            Console.WriteLine("\t Folder: {0} ({1})", wfInfo.FolderTitle, wfInfo.FolderId);
            Console.WriteLine("\t Upload time: {0}", wfInfo.UploadTime);
            Console.WriteLine("\t Last download time: {0}", wfInfo.LastDownloadTime);
            Console.WriteLine("\t Download count: {0}", wfInfo.DownloadCount);
        }
    }
}
